@@ -15,6 +15,8 @@ module Agents |
||
| 15 | 15 |
|
| 16 | 16 |
All rules must match for the Agent to match. The resulting Event will have a payload message of `message`. You can include extractions in the message, for example: `I saw a bar of: <foo.bar>` |
| 17 | 17 |
|
| 18 |
+ Set `keep_event` to `true` if you'd like to re-emit the incoming event, optionally merged with 'message' when provided. |
|
| 19 |
+ |
|
| 18 | 20 |
Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent. |
| 19 | 21 |
MD |
| 20 | 22 |
|
@@ -25,15 +27,20 @@ module Agents |
||
| 25 | 27 |
MD |
| 26 | 28 |
|
| 27 | 29 |
def validate_options |
| 28 |
- unless options['expected_receive_period_in_days'].present? && options['message'].present? && options['rules'].present? && |
|
| 30 |
+ unless options['expected_receive_period_in_days'].present? && options['rules'].present? && |
|
| 29 | 31 |
options['rules'].all? { |rule| rule['type'].present? && VALID_COMPARISON_TYPES.include?(rule['type']) && rule['value'].present? && rule['path'].present? }
|
| 30 | 32 |
errors.add(:base, "expected_receive_period_in_days, message, and rules, with a type, value, and path for every rule, are required") |
| 31 | 33 |
end |
| 34 |
+ |
|
| 35 |
+ errors.add(:base, "message is required unless 'keep_event' is 'true'") unless options['message'].present? || keep_event? |
|
| 36 |
+ |
|
| 37 |
+ errors.add(:base, "keep_event, when present, must be 'true' or 'false'") unless options['keep_event'].blank? || %w[true false].include?(options['keep_event']) |
|
| 32 | 38 |
end |
| 33 | 39 |
|
| 34 | 40 |
def default_options |
| 35 | 41 |
{
|
| 36 | 42 |
'expected_receive_period_in_days' => "2", |
| 43 |
+ 'keep_event' => 'false', |
|
| 37 | 44 |
'rules' => [{
|
| 38 | 45 |
'type' => "regex", |
| 39 | 46 |
'value' => "foo\\d+bar", |
@@ -79,10 +86,20 @@ module Agents |
||
| 79 | 86 |
end |
| 80 | 87 |
|
| 81 | 88 |
if match |
| 82 |
- create_event :payload => { 'message' => make_message(event[:payload]) } # Maybe this should include the
|
|
| 83 |
- # original event as well? |
|
| 89 |
+ if keep_event? |
|
| 90 |
+ payload = event.payload.dup |
|
| 91 |
+ payload['message'] = make_message(event[:payload]) if options['message'].present? |
|
| 92 |
+ else |
|
| 93 |
+ payload = { 'message' => make_message(event[:payload]) }
|
|
| 94 |
+ end |
|
| 95 |
+ |
|
| 96 |
+ create_event :payload => payload |
|
| 84 | 97 |
end |
| 85 | 98 |
end |
| 86 | 99 |
end |
| 100 |
+ |
|
| 101 |
+ def keep_event? |
|
| 102 |
+ options['keep_event'] == 'true' |
|
| 103 |
+ end |
|
| 87 | 104 |
end |
| 88 | 105 |
end |
@@ -30,9 +30,32 @@ describe Agents::TriggerAgent do |
||
| 30 | 30 |
@checker.should be_valid |
| 31 | 31 |
end |
| 32 | 32 |
|
| 33 |
- it "should validate presence of options" do |
|
| 33 |
+ it "should validate presence of message" do |
|
| 34 | 34 |
@checker.options['message'] = nil |
| 35 | 35 |
@checker.should_not be_valid |
| 36 |
+ |
|
| 37 |
+ @checker.options['message'] = '' |
|
| 38 |
+ @checker.should_not be_valid |
|
| 39 |
+ end |
|
| 40 |
+ |
|
| 41 |
+ it "should be valid without a message when 'keep_event' is set" do |
|
| 42 |
+ @checker.options['keep_event'] = 'true' |
|
| 43 |
+ @checker.options['message'] = '' |
|
| 44 |
+ @checker.should be_valid |
|
| 45 |
+ end |
|
| 46 |
+ |
|
| 47 |
+ it "if present, 'keep_event' must equal true or false" do |
|
| 48 |
+ @checker.options['keep_event'] = 'true' |
|
| 49 |
+ @checker.should be_valid |
|
| 50 |
+ |
|
| 51 |
+ @checker.options['keep_event'] = 'false' |
|
| 52 |
+ @checker.should be_valid |
|
| 53 |
+ |
|
| 54 |
+ @checker.options['keep_event'] = '' |
|
| 55 |
+ @checker.should be_valid |
|
| 56 |
+ |
|
| 57 |
+ @checker.options['keep_event'] = 'tralse' |
|
| 58 |
+ @checker.should_not be_valid |
|
| 36 | 59 |
end |
| 37 | 60 |
|
| 38 | 61 |
it "should validate the three fields in each rule" do |
@@ -278,5 +301,38 @@ describe Agents::TriggerAgent do |
||
| 278 | 301 |
@checker.receive([@event]) |
| 279 | 302 |
}.should_not change { Event.count }
|
| 280 | 303 |
end |
| 304 |
+ |
|
| 305 |
+ describe "when 'keep_event' is true" do |
|
| 306 |
+ before do |
|
| 307 |
+ @checker.options['keep_event'] = 'true' |
|
| 308 |
+ @event.payload['foo']['bar']['baz'] = "5" |
|
| 309 |
+ @checker.options['rules'].first['type'] = "field<value" |
|
| 310 |
+ end |
|
| 311 |
+ |
|
| 312 |
+ it "can re-emit the origin event" do |
|
| 313 |
+ @checker.options['rules'].first['value'] = 3 |
|
| 314 |
+ @checker.options['message'] = '' |
|
| 315 |
+ @event.payload['message'] = 'hi there' |
|
| 316 |
+ |
|
| 317 |
+ lambda {
|
|
| 318 |
+ @checker.receive([@event]) |
|
| 319 |
+ }.should_not change { Event.count }
|
|
| 320 |
+ |
|
| 321 |
+ @checker.options['rules'].first['value'] = 6 |
|
| 322 |
+ lambda {
|
|
| 323 |
+ @checker.receive([@event]) |
|
| 324 |
+ }.should change { Event.count }.by(1)
|
|
| 325 |
+ |
|
| 326 |
+ @checker.most_recent_event.payload.should == @event.payload |
|
| 327 |
+ end |
|
| 328 |
+ |
|
| 329 |
+ it "merges 'message' into the original event when present" do |
|
| 330 |
+ @checker.options['rules'].first['value'] = 6 |
|
| 331 |
+ |
|
| 332 |
+ @checker.receive([@event]) |
|
| 333 |
+ |
|
| 334 |
+ @checker.most_recent_event.payload.should == @event.payload.merge(:message => "I saw '5' from Joe") |
|
| 335 |
+ end |
|
| 336 |
+ end |
|
| 281 | 337 |
end |
| 282 | 338 |
end |